package com.customization.unionpay.market.service.impl;

import cn.hutool.core.util.StrUtil;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.service.MarketingRefundService;
import com.customization.unionpay.market.util.PropUtils;
import com.engine.core.impl.Service;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

public class MarketingRefundServiceImpl extends Service implements MarketingRefundService {

    private static final Logger log = LoggerFactory.getLogger(MarketingRefundServiceImpl.class);

    @Override
    public synchronized boolean externalUpdatePayState(String actAuthCode, String refundCode, String paymentStatus) throws Exception {
        boolean res = false;

        RecordSet rs = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();

        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            // 营销退款明细台账表单id
            int marketingRefundDetailsFormId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_refund_details_form_id"));
            // 获取营销退款明细台账主表表名
            String marketingRefundDetailsMainTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(marketingRefundDetailsFormId);
            log.info("获取营销退款明细台账主表表名："+marketingRefundDetailsMainTableName);
            if (StrUtil.isNotBlank(marketingRefundDetailsMainTableName)) {
                // 查询营销项目信息台账
                String sql = "select id from #{tableName} where tkdh = ? and yxpthdsqm = ? ";
                sql = sql.replace("#{tableName}", marketingRefundDetailsMainTableName);
                rs.executeQuery(sql,refundCode,actAuthCode);
                log.info("sql：" + sql);
                if (rs.next()) {
                    String id = Util.null2String(rs.getString("id"));
                    try {
                        // 修改营销项目信息台账
                        String updateSql = "update #{tableName} set zfzt=? where id  = ? ";
                        updateSql = updateSql.replace("#{tableName}", marketingRefundDetailsMainTableName);
                        log.info("修改sql：" + updateSql);
                        rst.executeUpdate(updateSql,paymentStatus,id);
                        // 提交事务
                        boolean commit = rst.commit();
                        if (commit) {
                            res = true;
                        }
                        return res;
                    } catch (Exception e) {
                        // 回滚
                        rst.rollback();
                        throw new Exception(e.getMessage());
                    }
                } else {
                    throw new Exception("根据授权码和退款单号查询营销退款明细台账为空！");
                }
            } else {
                throw new Exception("根据营销退款明细台账formId查询数据库表名失败！");
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    @Override
    public String updatePayState(String ids, String state, User user) throws Exception {
        ids = ids.replace(",","','");
        log.info("ids:"+ids);
        String result = null;
        // 营销退款明细台账表单id
        int marketingRefundDetailsFormId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_refund_details_form_id"));
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            String tableName = FormDataUtils.getTableName(marketingRefundDetailsFormId);
            log.info("tableName："+tableName);
            // 根据当前信息使用id修改新建数据
            String updateSql = "update #{tableName} set  zfzt=? where id in ('"+ids+"')";
            updateSql = updateSql.replace("#{tableName}", tableName);
            log.info("修改sql：" + updateSql);
            rst.executeUpdate(updateSql,state);
            // 提交事务
            rst.commit();
        } catch (Exception e) {
            // 回滚
            rst.rollback();
            log.error("修改数据异常：" + e.getMessage());
            throw new Exception(e.getMessage());
        }
        return result;
    }


}
